﻿using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;

namespace Valve.VR.InteractionSystem
{
	// Token: 0x0200061B RID: 1563
	[RequireComponent(typeof(Interactable))]
	public class ItemPackageSpawner : MonoBehaviour
	{
		// Token: 0x170004CB RID: 1227
		// (get) Token: 0x06002786 RID: 10118 RVA: 0x000E63A8 File Offset: 0x000E45A8
		// (set) Token: 0x06002787 RID: 10119 RVA: 0x000E63B0 File Offset: 0x000E45B0
		public ItemPackage itemPackage
		{
			get
			{
				return this._itemPackage;
			}
			set
			{
				this.CreatePreviewObject();
			}
		}

		// Token: 0x06002788 RID: 10120 RVA: 0x000E63B8 File Offset: 0x000E45B8
		private void CreatePreviewObject()
		{
			if (!this.useItemPackagePreview)
			{
				return;
			}
			this.ClearPreview();
			if (this.useItemPackagePreview)
			{
				if (this.itemPackage == null)
				{
					return;
				}
				if (!this.useFadedPreview)
				{
					if (this.itemPackage.previewPrefab != null)
					{
						this.previewObject = UnityEngine.Object.Instantiate<GameObject>(this.itemPackage.previewPrefab, base.transform.position, Quaternion.identity);
						this.previewObject.transform.parent = base.transform;
						this.previewObject.transform.localRotation = Quaternion.identity;
					}
				}
				else if (this.itemPackage.fadedPreviewPrefab != null)
				{
					this.previewObject = UnityEngine.Object.Instantiate<GameObject>(this.itemPackage.fadedPreviewPrefab, base.transform.position, Quaternion.identity);
					this.previewObject.transform.parent = base.transform;
					this.previewObject.transform.localRotation = Quaternion.identity;
				}
			}
		}

		// Token: 0x06002789 RID: 10121 RVA: 0x000E64D4 File Offset: 0x000E46D4
		private void Start()
		{
			this.VerifyItemPackage();
		}

		// Token: 0x0600278A RID: 10122 RVA: 0x000E64DC File Offset: 0x000E46DC
		private void VerifyItemPackage()
		{
			if (this.itemPackage == null)
			{
				this.ItemPackageNotValid();
			}
			if (this.itemPackage.itemPrefab == null)
			{
				this.ItemPackageNotValid();
			}
		}

		// Token: 0x0600278B RID: 10123 RVA: 0x000E6514 File Offset: 0x000E4714
		private void ItemPackageNotValid()
		{
			GlobalData.Debug_LogError("ItemPackage assigned to " + base.gameObject.name + " is not valid. Destroying this game object.");
			UnityEngine.Object.Destroy(base.gameObject);
		}

		// Token: 0x0600278C RID: 10124 RVA: 0x000E6540 File Offset: 0x000E4740
		private void ClearPreview()
		{
			IEnumerator enumerator = base.transform.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					object obj = enumerator.Current;
					Transform transform = (Transform)obj;
					if (Time.time > 0f)
					{
						UnityEngine.Object.Destroy(transform.gameObject);
					}
					else
					{
						UnityEngine.Object.DestroyImmediate(transform.gameObject);
					}
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}

		// Token: 0x0600278D RID: 10125 RVA: 0x000E65C8 File Offset: 0x000E47C8
		private void Update()
		{
			if (this.itemIsSpawned && this.spawnedItem == null)
			{
				this.itemIsSpawned = false;
				this.useFadedPreview = false;
				this.dropEvent.Invoke();
				this.CreatePreviewObject();
			}
		}

		// Token: 0x0600278E RID: 10126 RVA: 0x000E6608 File Offset: 0x000E4808
		private void OnHandHoverBegin(Hand hand)
		{
			ItemPackage attachedItemPackage = this.GetAttachedItemPackage(hand);
			if (attachedItemPackage == this.itemPackage && this.takeBackItem && !this.requireTriggerPressToReturn)
			{
				this.TakeBackItem(hand);
			}
			if (!this.requireTriggerPressToTake)
			{
				this.SpawnAndAttachObject(hand);
			}
			if (this.requireTriggerPressToTake && this.showTriggerHint)
			{
				ControllerButtonHints.ShowTextHint(hand, EVRButtonId.k_EButton_Axis1, "PickUp", true);
			}
		}

		// Token: 0x0600278F RID: 10127 RVA: 0x000E6684 File Offset: 0x000E4884
		private void TakeBackItem(Hand hand)
		{
			this.RemoveMatchingItemsFromHandStack(this.itemPackage, hand);
			if (this.itemPackage.packageType == ItemPackage.ItemPackageType.TwoHanded)
			{
				this.RemoveMatchingItemsFromHandStack(this.itemPackage, hand.otherHand);
			}
		}

		// Token: 0x06002790 RID: 10128 RVA: 0x000E66B8 File Offset: 0x000E48B8
		private ItemPackage GetAttachedItemPackage(Hand hand)
		{
			GameObject currentAttachedObject = hand.currentAttachedObject;
			if (currentAttachedObject == null)
			{
				return null;
			}
			ItemPackageReference component = hand.currentAttachedObject.GetComponent<ItemPackageReference>();
			if (component == null)
			{
				return null;
			}
			return component.itemPackage;
		}

		// Token: 0x06002791 RID: 10129 RVA: 0x000E66FC File Offset: 0x000E48FC
		private void HandHoverUpdate(Hand hand)
		{
			if (this.requireTriggerPressToTake && hand.controller != null && hand.controller.GetHairTriggerDown())
			{
				this.SpawnAndAttachObject(hand);
			}
		}

		// Token: 0x06002792 RID: 10130 RVA: 0x000E672C File Offset: 0x000E492C
		private void OnHandHoverEnd(Hand hand)
		{
			if (!this.justPickedUpItem && this.requireTriggerPressToTake && this.showTriggerHint)
			{
				ControllerButtonHints.HideTextHint(hand, EVRButtonId.k_EButton_Axis1);
			}
			this.justPickedUpItem = false;
		}

		// Token: 0x06002793 RID: 10131 RVA: 0x000E6760 File Offset: 0x000E4960
		private void RemoveMatchingItemsFromHandStack(ItemPackage package, Hand hand)
		{
			for (int i = 0; i < hand.AttachedObjects.Count; i++)
			{
				ItemPackageReference component = hand.AttachedObjects[i].attachedObject.GetComponent<ItemPackageReference>();
				if (component != null)
				{
					ItemPackage itemPackage = component.itemPackage;
					if (itemPackage != null && itemPackage == package)
					{
						GameObject attachedObject = hand.AttachedObjects[i].attachedObject;
						hand.DetachObject(attachedObject, true);
					}
				}
			}
		}

		// Token: 0x06002794 RID: 10132 RVA: 0x000E67F0 File Offset: 0x000E49F0
		private void RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType packageType, Hand hand)
		{
			for (int i = 0; i < hand.AttachedObjects.Count; i++)
			{
				ItemPackageReference component = hand.AttachedObjects[i].attachedObject.GetComponent<ItemPackageReference>();
				if (component != null && component.itemPackage.packageType == packageType)
				{
					GameObject attachedObject = hand.AttachedObjects[i].attachedObject;
					hand.DetachObject(attachedObject, true);
				}
			}
		}

		// Token: 0x06002795 RID: 10133 RVA: 0x000E6870 File Offset: 0x000E4A70
		private void SpawnAndAttachObject(Hand hand)
		{
			if (hand.otherHand != null)
			{
				ItemPackage attachedItemPackage = this.GetAttachedItemPackage(hand.otherHand);
				if (attachedItemPackage == this.itemPackage)
				{
					this.TakeBackItem(hand.otherHand);
				}
			}
			if (this.showTriggerHint)
			{
				ControllerButtonHints.HideTextHint(hand, EVRButtonId.k_EButton_Axis1);
			}
			if (this.itemPackage.otherHandItemPrefab != null && hand.otherHand.hoverLocked)
			{
				return;
			}
			if (this.itemPackage.packageType == ItemPackage.ItemPackageType.OneHanded)
			{
				this.RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.OneHanded, hand);
				this.RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand);
				this.RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand.otherHand);
			}
			if (this.itemPackage.packageType == ItemPackage.ItemPackageType.TwoHanded)
			{
				this.RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.OneHanded, hand);
				this.RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.OneHanded, hand.otherHand);
				this.RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand);
				this.RemoveMatchingItemTypesFromHand(ItemPackage.ItemPackageType.TwoHanded, hand.otherHand);
			}
			this.spawnedItem = UnityEngine.Object.Instantiate<GameObject>(this.itemPackage.itemPrefab);
			this.spawnedItem.SetActive(true);
			hand.AttachObject(this.spawnedItem, this.attachmentFlags, this.attachmentPoint);
			if (this.itemPackage.otherHandItemPrefab != null && hand.otherHand.controller != null)
			{
				GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.itemPackage.otherHandItemPrefab);
				gameObject.SetActive(true);
				hand.otherHand.AttachObject(gameObject, this.attachmentFlags, string.Empty);
			}
			this.itemIsSpawned = true;
			this.justPickedUpItem = true;
			if (this.takeBackItem)
			{
				this.useFadedPreview = true;
				this.pickupEvent.Invoke();
				this.CreatePreviewObject();
			}
		}

		// Token: 0x04002784 RID: 10116
		public ItemPackage _itemPackage;

		// Token: 0x04002785 RID: 10117
		private bool useItemPackagePreview = true;

		// Token: 0x04002786 RID: 10118
		private bool useFadedPreview;

		// Token: 0x04002787 RID: 10119
		private GameObject previewObject;

		// Token: 0x04002788 RID: 10120
		public bool requireTriggerPressToTake;

		// Token: 0x04002789 RID: 10121
		public bool requireTriggerPressToReturn;

		// Token: 0x0400278A RID: 10122
		public bool showTriggerHint;

		// Token: 0x0400278B RID: 10123
		[EnumFlags]
		public Hand.AttachmentFlags attachmentFlags = Hand.AttachmentFlags.SnapOnAttach | Hand.AttachmentFlags.DetachOthers | Hand.AttachmentFlags.DetachFromOtherHand | Hand.AttachmentFlags.ParentToHand;

		// Token: 0x0400278C RID: 10124
		public string attachmentPoint;

		// Token: 0x0400278D RID: 10125
		public bool takeBackItem;

		// Token: 0x0400278E RID: 10126
		public bool acceptDifferentItems;

		// Token: 0x0400278F RID: 10127
		private GameObject spawnedItem;

		// Token: 0x04002790 RID: 10128
		private bool itemIsSpawned;

		// Token: 0x04002791 RID: 10129
		public UnityEvent pickupEvent;

		// Token: 0x04002792 RID: 10130
		public UnityEvent dropEvent;

		// Token: 0x04002793 RID: 10131
		public bool justPickedUpItem;
	}
}
